<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
abstract class Salmon_Abstract {
    protected function postSalmon($salmonEndpoint, $envelope) {
        
        //echo "\n\n\tSending: '" . $envelope . "'.";
        //echo "\n\n\tTo     : '" . $salmonEndpoint . "'.";
        file_put_contents("/tmp/minime-salmon.post", $envelope);
        $headers[] = 'Content-type: application/magic-envelope+xml'; 
        
        $request = curl_init($salmonEndpoint);
        curl_setopt($request, CURLOPT_POST, TRUE);
        curl_setopt($request, CURLOPT_POSTFIELDS, $envelope);
        curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($request, CURLOPT_HTTPHEADER, $headers); 
        $body = curl_exec($request);
        $code = curl_getinfo($request, CURLINFO_HTTP_CODE);
        $ret = true;
    
        if ( !($code >= 200 && $code < 400) ) {
            $ret = false;
        }
        
        file_put_contents("/tmp/minime-salmon-result.error", $salmonEndpoint . ":" . $code . ":" . $body ."\n");
        //echo "\n\n\tbody   : '" . $body . "'.";
        //echo "\n\n\tcode   : '" . $code . "'.";
        
        curl_close($request);
        return $ret;    
    }
}